package com.xiaobai.frame.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class ThreadPoolConfig {
    @Bean("xiaobai")
    public Executor asyncServiceExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5); // 核心线程数
        executor.setMaxPoolSize(20); // 最大线程数
        executor.setQueueCapacity(Integer.MAX_VALUE); // 队列大小
        executor.setKeepAliveSeconds(60); // 线程活跃时间
        executor.setThreadNamePrefix("小白"); // 线程名称
        executor.setWaitForTasksToCompleteOnShutdown(true); // 等待任务结束关闭线程池
        executor.initialize(); // 初始化线程池
        return executor;
    }
}
